import re
from src.tools.Translated import Translated
from src.tools.extractCode import extract
from src.parse_project.isdao import class_main


# 对java文件进行分类
def classify(path):
    new_path = Translated(path)
    file_code = extract(new_path)
    # 正则表达式
    pattern_controller = r"(^|\s)\@RestController(\s|$)"
    pattern_mapper = r"(^|\s)\@Mapper(\s|$)"
    pattern_service = r"(^|\s)\@Service(\s|$)"
    # 判断
    if re.search(pattern_controller, file_code):
        return "Controller"
    elif re.search(pattern_mapper, file_code):
        return "Mapper"
    elif re.search(pattern_service, file_code):
        return "Service"
    else:
        # 使用llm判断是否为实体类
        res_data = class_main(new_path)
        # print(res_data)
        # print(res_data.content)
        # print(type(res_data))
        if res_data.content == "true":
            return "Dao"
    return None


# 测试的main函数
if __name__ == '__main__':
    # file_path = r"../../java\project\backend\src\main\java\com\open\harmony\mapper\ProductMapper.java"
    file_path = r"../../java\project\backend\src\main\java\com\open\harmony\entity\Order.java"
    # file_path = r"../../java\project\backend\src\main\java\com\open\harmony\util\FakeDataGenerator.java"
    res_data = classify(file_path)
    print(res_data)
